/**
 * Project Name:PayApiController.java
 * File Name:PayApiController.java
 * Date:2018/7/28 上午9:35
 * Copyright (c) ${year}, zhang.xiangyu@foxmail.com All Rights Reserved.
*/
package com.meatball.api.pay.controller;

import com.meatball.api.pay.params.PlaceOrderParams;
import com.meatball.api.pay.service.PayApiService;
import com.meatball.component.OperateLog;
import com.meatball.vo.ResultMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Title: PayApiController.java
 * @Description: todo(外部调用统一支付接口)
 * @Author 張翔宇
 * @Date 2018/7/28 上午9:36
 * @Version V1.0
 */
@Api(tags = "统一支付接口")
@Slf4j
@Controller
@RequestMapping("/api/pay")
public class PayApiController {
    @Resource
    private PayApiService service;

    /**
     * @Title: placeOrder
     * @Description: todo(统一下单接口)
     * @Params: [record]
     * @Return com.meatball.vo.ResultMsg    返回类型
     * @Author 張翔宇
     * @Date 2018/7/28 上午9:42
     */
    @ApiOperation(value = "统一下单", notes = "返回：扫码支付信息")
    @ApiResponse(code = 200, message = "权限验证成功", response = PlaceOrderParams.class)
    @OperateLog("移动下单")
    @ResponseBody
    @PostMapping("/placeorder")
    public ResultMsg placeOrder(@RequestBody PlaceOrderParams record) {
        return service.placeOrder(record);
    }

    /**
     * @Title: payment
     * @Description: todo(扫码支付接口)
     * @Params:
     * @Return     返回类型
     * @Author 張翔宇
     * @Date 2018/7/30 上午12:43
     */
    @ApiIgnore
//    @OperateLog("二维码扫码支付")
    @GetMapping("/{id}")
    public String payment(@PathVariable Long id, HttpServletRequest request, ModelMap modelMap) throws Exception {
        ResultMsg resultMsg = service.payment(id, request.getHeader("User-Agent"));
        modelMap.put("result", resultMsg);
        return "pay/order/qrPay";
    }

    /**
     * @Title: notifyUrl
     * @Description: todo(支付回調-异步通知)
     * @Params:
     * @Return     返回类型
     * @Author 張翔宇
     * @Date 2018/7/30 下午10:59
     */
    @ApiIgnore
    @PostMapping("/alipayasynccallback")
    @ResponseBody
    public String alipayAsyncCallback(HttpServletRequest request) throws Exception {
        service.alipayCallback(request);
        return "success";
    }

    /**
     * @Title: paySuccess
     * @Description: todo(支付回调-同步通知)
     * @Params:
     * @Return     返回类型
     * @Author 張翔宇
     * @Date 2018/7/31 下午7:46
     */
    @ApiIgnore
    @GetMapping("/paysyncsuccess")
    public String paySyncSuccess(HttpServletRequest request, ModelMap modelMap) throws Exception {
        ResultMsg resultMsg = service.paySyncSuccess(request);
        modelMap.put("result", resultMsg);
        return "pay/payment/PaySuccess";
    }
}
